package com.lin.manager.sms.config;

import com.lin.manager.sms.config.properties.SmsProperties;
import com.lin.manager.sms.factory.SmsFactory;
import com.lin.manager.sms.model.Sms;
import com.lin.manager.sms.strategy.SendStrategy;
import com.lin.manager.sms.strategy.SmsStrategy;
import com.lin.manager.sms.strategy.service.aliyun.AlibabaSmsStrategy;
import com.lin.manager.sms.strategy.service.local.ControlStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author linjiang
 * @date 2024/8/16
 * @fn
 */
@Configuration
@Import(SmsProperties.class)
public class SendConfig {

    @Resource
    private SmsProperties smsProperties;

    @Bean
    public SmsFactory smsFactory() {
        String use = smsProperties.getUse();
        Map<String, Sms> smsMap = smsProperties.getPlatfrom();
        smsMap.forEach((k, v) -> v.setPlatform(k));
        SmsFactory factory = new SmsFactory(smsMap.get(use));
        factory.addStrategy(getStrategys(factory));
        return factory;
    }

    @Bean
    public SmsStrategy smsStrategy() {
        return new SmsStrategy(smsFactory());
    }

    /**
     * 统一获取策略
     *
     * @return
     */
    private List<SendStrategy> getStrategys(SmsFactory factory) {
        List<SendStrategy> sendStrategies = new ArrayList<>();
        sendStrategies.add(new ControlStrategy());
        sendStrategies.add(new AlibabaSmsStrategy(factory));

        return sendStrategies;
    }


}
